using System;
using System.IO;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Rename
{
	public class MainForm : System.Windows.Forms.Form
	{
		#region WinForms Plumbing;
        internal ColumnHeader ctlColumnName;
        internal ColumnHeader ctlColumnSize;
        internal ColumnHeader ctlColumnDateModified;
        internal ColumnHeader ctlColumnFullPath;
        internal ListView ctlFileList;
        private Button ctlClear;
        private Button ctlRename;
        private StatusBar ctlStatus;
        private StatusBarPanel ctlPanel1;
        private Label ctlReplaceLabel;
        private Label ctlSearchLabel;
        private StatusBarPanel ctlPanel2;
        private TextBox ctlReplace;
        private TextBox ctlSearch;
        /// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public MainForm()
		{
			InitializeComponent();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.ctlFileList = new System.Windows.Forms.ListView();
            this.ctlColumnName = new System.Windows.Forms.ColumnHeader();
            this.ctlColumnSize = new System.Windows.Forms.ColumnHeader();
            this.ctlColumnDateModified = new System.Windows.Forms.ColumnHeader();
            this.ctlColumnFullPath = new System.Windows.Forms.ColumnHeader();
            this.ctlSearch = new System.Windows.Forms.TextBox();
            this.ctlRename = new System.Windows.Forms.Button();
            this.ctlClear = new System.Windows.Forms.Button();
            this.ctlStatus = new System.Windows.Forms.StatusBar();
            this.ctlPanel1 = new System.Windows.Forms.StatusBarPanel();
            this.ctlPanel2 = new System.Windows.Forms.StatusBarPanel();
            this.ctlSearchLabel = new System.Windows.Forms.Label();
            this.ctlReplace = new System.Windows.Forms.TextBox();
            this.ctlReplaceLabel = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.ctlPanel1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.ctlPanel2)).BeginInit();
            this.SuspendLayout();
            // 
            // ctlFileList
            // 
            this.ctlFileList.AllowDrop = true;
            this.ctlFileList.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.ctlColumnName,
            this.ctlColumnSize,
            this.ctlColumnDateModified,
            this.ctlColumnFullPath});
            this.ctlFileList.Font = new System.Drawing.Font("Segoe UI", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.ctlFileList.FullRowSelect = true;
            this.ctlFileList.GridLines = true;
            this.ctlFileList.Location = new System.Drawing.Point(10, 10);
            this.ctlFileList.Name = "ctlFileList";
            this.ctlFileList.Size = new System.Drawing.Size(775, 338);
            this.ctlFileList.Sorting = System.Windows.Forms.SortOrder.Ascending;
            this.ctlFileList.TabIndex = 4;
            this.ctlFileList.UseCompatibleStateImageBehavior = false;
            this.ctlFileList.View = System.Windows.Forms.View.Details;
            this.ctlFileList.DragEnter += new System.Windows.Forms.DragEventHandler(this.ctlFileList_DragEnter);
            this.ctlFileList.DragDrop += new System.Windows.Forms.DragEventHandler(this.ctlFileList_DragDrop);
            // 
            // ctlColumnName
            // 
            this.ctlColumnName.Text = "Name";
            this.ctlColumnName.Width = 160;
            // 
            // ctlColumnSize
            // 
            this.ctlColumnSize.Text = "Size";
            this.ctlColumnSize.Width = 100;
            // 
            // ctlColumnDateModified
            // 
            this.ctlColumnDateModified.Text = "Date Modified";
            this.ctlColumnDateModified.Width = 150;
            // 
            // ctlColumnFullPath
            // 
            this.ctlColumnFullPath.Text = "Path";
            this.ctlColumnFullPath.Width = 335;
            // 
            // ctlSearch
            // 
            this.ctlSearch.Location = new System.Drawing.Point(79, 356);
            this.ctlSearch.MaxLength = 25;
            this.ctlSearch.Name = "ctlSearch";
            this.ctlSearch.Size = new System.Drawing.Size(220, 22);
            this.ctlSearch.TabIndex = 1;
            this.ctlSearch.Text = "Old";
            // 
            // ctlRename
            // 
            this.ctlRename.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.ctlRename.Font = new System.Drawing.Font("Segoe UI", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.ctlRename.Location = new System.Drawing.Point(629, 354);
            this.ctlRename.Name = "ctlRename";
            this.ctlRename.Size = new System.Drawing.Size(75, 24);
            this.ctlRename.TabIndex = 3;
            this.ctlRename.Text = "Rename";
            this.ctlRename.UseVisualStyleBackColor = true;
            this.ctlRename.Click += new System.EventHandler(this.ctlRename_Click);
            // 
            // ctlClear
            // 
            this.ctlClear.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.ctlClear.Font = new System.Drawing.Font("Segoe UI", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.ctlClear.Location = new System.Drawing.Point(710, 354);
            this.ctlClear.Name = "ctlClear";
            this.ctlClear.Size = new System.Drawing.Size(75, 24);
            this.ctlClear.TabIndex = 4;
            this.ctlClear.Text = "Clear";
            this.ctlClear.Click += new System.EventHandler(this.ctlClear_Click);
            // 
            // ctlStatus
            // 
            this.ctlStatus.Location = new System.Drawing.Point(0, 384);
            this.ctlStatus.Name = "ctlStatus";
            this.ctlStatus.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
            this.ctlPanel1,
            this.ctlPanel2});
            this.ctlStatus.ShowPanels = true;
            this.ctlStatus.Size = new System.Drawing.Size(794, 22);
            this.ctlStatus.SizingGrip = false;
            this.ctlStatus.TabIndex = 6;
            // 
            // ctlPanel1
            // 
            this.ctlPanel1.Name = "ctlPanel1";
            this.ctlPanel1.Width = 755;
            // 
            // ctlPanel2
            // 
            this.ctlPanel2.Alignment = System.Windows.Forms.HorizontalAlignment.Center;
            this.ctlPanel2.Name = "ctlPanel2";
            this.ctlPanel2.Width = 37;
            // 
            // ctlSearchLabel
            // 
            this.ctlSearchLabel.AutoSize = true;
            this.ctlSearchLabel.Location = new System.Drawing.Point(12, 359);
            this.ctlSearchLabel.Name = "ctlSearchLabel";
            this.ctlSearchLabel.Size = new System.Drawing.Size(61, 13);
            this.ctlSearchLabel.TabIndex = 7;
            this.ctlSearchLabel.Text = "Search For";
            // 
            // ctlReplace
            // 
            this.ctlReplace.Location = new System.Drawing.Point(389, 356);
            this.ctlReplace.MaxLength = 25;
            this.ctlReplace.Name = "ctlReplace";
            this.ctlReplace.Size = new System.Drawing.Size(220, 22);
            this.ctlReplace.TabIndex = 2;
            this.ctlReplace.Text = "New";
            // 
            // ctlReplaceLabel
            // 
            this.ctlReplaceLabel.AutoSize = true;
            this.ctlReplaceLabel.Location = new System.Drawing.Point(308, 359);
            this.ctlReplaceLabel.Name = "ctlReplaceLabel";
            this.ctlReplaceLabel.Size = new System.Drawing.Size(75, 13);
            this.ctlReplaceLabel.TabIndex = 7;
            this.ctlReplaceLabel.Text = "Replace With";
            // 
            // MainForm
            // 
            this.AccessibleDescription = "";
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
            this.ClientSize = new System.Drawing.Size(794, 406);
            this.Controls.Add(this.ctlReplaceLabel);
            this.Controls.Add(this.ctlSearchLabel);
            this.Controls.Add(this.ctlStatus);
            this.Controls.Add(this.ctlClear);
            this.Controls.Add(this.ctlRename);
            this.Controls.Add(this.ctlReplace);
            this.Controls.Add(this.ctlSearch);
            this.Controls.Add(this.ctlFileList);
            this.Font = new System.Drawing.Font("Segoe UI", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.HelpButton = true;
            this.MaximizeBox = false;
            this.Name = "MainForm";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Pattern Rename";
            this.Load += new System.EventHandler(this.MainForm_Load);
            ((System.ComponentModel.ISupportInitialize)(this.ctlPanel1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.ctlPanel2)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		#endregion
		
		static void Main()
		{
			Application.Run(new MainForm());
		}
		private bool FileInList(string FileName)
		{
			foreach (ListViewItem Item in ctlFileList.Items)
			{
				if (Item.Text == FileName)
				{
					return true;
				}
			}
			return false;
		}
		private string FormatFileSize(string FileSize)
		{
			FileSize = (int.Parse(FileSize) / 1000).ToString() + " KB";
			return FileSize;
		}
		private string CleanFileName(string FileName)
		{
			FileName = FileName.Replace("\\", "");
			FileName = FileName.Replace("/", "");
			FileName = FileName.Replace("|", "");
			FileName = FileName.Replace("\"", "");
			FileName = FileName.Replace("<", "");
			FileName = FileName.Replace(">", "");
			FileName = FileName.Replace("?", "");
			return FileName;
		}
		private void AddFileToList(string FileName, string FileSize, string FileDateModified, string FileFullPath)
		{
			ctlFileList.BeginUpdate();
			ListViewItem Item = new ListViewItem(FileName);
			Item.SubItems.Add(FormatFileSize(FileSize));
			Item.SubItems.Add(FileDateModified);
			Item.SubItems.Add(FileFullPath);
			ctlFileList.Items.Add(Item);
			ctlFileList.EndUpdate();
		}
		private void RenameFile(string ExistingFileName, string NewFileName)
		{
			FileInfo File = new FileInfo(ExistingFileName);
			if (File.Exists)
			{
				//File.MoveTo(File.DirectoryName + "\\" + CleanFileName(NewFileName) + File.Extension);
                File.MoveTo(NewFileName);
			}
		}
		private void MainForm_Load(object sender, System.EventArgs e)
		{
			ctlFileList.Items.Clear();
		}
		private void ctlFileList_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
		{
			string[] Files = (string[])e.Data.GetData(DataFormats.FileDrop);
			FileInfo File;
			foreach(string FilePath in Files)
			{
				File = new FileInfo(FilePath);
				if (!FileInList(File.Name))
				{
					AddFileToList(File.Name, File.Length.ToString(), File.LastWriteTime.ToString(), FilePath);
				}
			}
		}
		private void ctlFileList_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
		{
			e.Effect = DragDropEffects.Move;
		}
		private void ctlClear_Click(object sender, System.EventArgs e)
		{
			ctlFileList.Items.Clear();
		}
        private void ctlRename_Click(object sender, EventArgs e)
        {
            string RenameFrom = "";
            string RenameTo = "";
            string SearchFor = ctlSearch.Text;
            string ReplaceWith = ctlReplace.Text;

            foreach (ListViewItem Item in ctlFileList.Items)
            {
                RenameFrom = Item.SubItems[3].Text;

                if (RenameFrom.Contains(SearchFor))
                {
                    RenameTo = RenameFrom.Replace(SearchFor, ReplaceWith);
                    RenameFile(RenameFrom, RenameTo);
                }
            }
        }
	}
}
